package cn.lanqiao.bigDate.service.data.impl;

import cn.lanqiao.bigDate.entity.data.DataPbhouse;
import cn.lanqiao.bigDate.mapper.data.DataPbhouseMapper;
import cn.lanqiao.bigDate.service.data.DataPbhouseService;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Service
public class DataPbhouseServiceImpl implements DataPbhouseService {
    @Resource
    private DataPbhouseMapper dataPbhouseMapper;

    @Override
    public IPage<DataPbhouse> findByPage(Page<DataPbhouse> page, String searchDeptName) {
        QueryWrapper<DataPbhouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("id");
        queryWrapper.like("pbhouse_name",searchDeptName);
        Page<DataPbhouse> dataPbhousePage = dataPbhouseMapper.selectPage(page,queryWrapper);
        return dataPbhousePage;
    }

    @Override
    public DataPbhouse findDataPbhouseOne(Long id) {
        QueryWrapper<DataPbhouse> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        DataPbhouse dataPbhouse = dataPbhouseMapper.selectOne(wrapper);
        return dataPbhouse;
    }

    @Override
    public Boolean updateDataPbhouse(DataPbhouse dataPbhouse) {
        if (dataPbhouse != null && dataPbhouse.getId()!=null) {
            Long id = dataPbhouse.getId();
            DataPbhouse dataPbhouseOne = findDataPbhouseOne(id);
            dataPbhouseOne.setPbhouseName(dataPbhouse.getPbhouseName());
            dataPbhouseOne.setMarketShare(dataPbhouse.getMarketShare());
            dataPbhouseOne.setBestSeller(dataPbhouse.getBestSeller());
            dataPbhouseOne.setNewShare(dataPbhouse.getNewShare());
            dataPbhouseOne.setNewContribution(dataPbhouse.getNewContribution());
            dataPbhouseOne.setPopular(dataPbhouse.getPopular());
            dataPbhouseOne.setTotalBatch(dataPbhouse.getTotalBatch());
            dataPbhouseOne.setUpdateTime(LocalDateTime.now());
            int i = dataPbhouseMapper.updateById(dataPbhouse);
            if(i==1){
                return true;
            }
            return false;
        }
        return false;
    }

    @Override
    public Boolean saveDataPbhouse(DataPbhouse dataPbhouse) {
        if(dataPbhouse.getPbhouseName()!=null&&dataPbhouse.getBestSeller()!=null&&dataPbhouse.getMarketShare()!=null&&dataPbhouse.getNewShare()!=null&&dataPbhouse.getNewContribution()!=null&&dataPbhouse.getPopular()!=null&&dataPbhouse.getPopular()!=null){
            dataPbhouse.setCreateTime(LocalDateTime.now());
            int insert = dataPbhouseMapper.insert(dataPbhouse);
            if(insert==1){
                return true;
            }
        }
        return false;
    }

    @Override
    public Boolean deleteDataPbhouse(String ids) {
        if(!StringUtils.isEmpty(ids)){
            String[] id = ids.split(",");
            System.out.println(Arrays.toString(id));
            List<String> strings = Arrays.asList(id); //将数组转换成列表
            int i = dataPbhouseMapper.deleteBatchIds(strings);
            if(i>0){
                return true;
            }
        }
        return false;
    }

    @Override
    public List<DataPbhouse> selectByPbhouseName(String name) {

        QueryWrapper<DataPbhouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("pbhouse_number",name);
        List<DataPbhouse> dataPbhouses = dataPbhouseMapper.selectList(queryWrapper);
        return dataPbhouses;
    }
}
